import { http } from "@/utils/http";
import type {
  ArticleVo,
  BlogVo,
  HotArticlesVo,
  RecentArticlesVo,
  TreeNode,
} from "@/type";

interface ArticleReq {
  count: number;
  rows: BlogVo[];
}

interface PostReq {
  getArticle: (params: any) => Promise<ArticleReq>;
  getRecent: () => Promise<RecentArticlesVo[]>;
  getPost: (
    id: number,
    password?: string | null,
  ) => Promise<{ code: number; data: ArticleVo; message?: string }>;
  search: (params: any) => Promise<ArticleReq>;
  getArchive: () => Promise<{ date: TreeNode[] }>;
  getTimeline: (params: any) => Promise<{ rows: any[]; count: number }>;
  setLike: (id: number) => Promise<{ status: boolean; message: string }>;
  setSubscribe: (params: any) => Promise<{ status: boolean; message: string }>;
  getHot: () => Promise<HotArticlesVo[]>;
}

export const postApi: PostReq = {
  getArticle: (params: any) => {
    return http.get<ArticleReq>("/post/getArticle", { params });
  },
  getRecent: () => {
    return http.get<RecentArticlesVo[]>("/post/recent");
  },
  getPost: (id: number, password?: string | null) => {
    return http.get<{ code: number; data: ArticleVo; message?: string }>(
      `/post/getfront/${id}/${password}`,
    );
  },
  search: (params: any) => {
    return http.get<any>("/post/search", { params });
  },
  getArchive: () => {
    return http.get<any>("/post/archive");
  },
  getTimeline: (params: any) => {
    return http.get<{ rows: any[]; count: number }>("/post/timeline", {
      params,
    });
  },
  setLike: (id: number) => {
    return http.post<{ status: boolean; message: string }>(`/post/like`, {
      post_id: id,
    });
  },
  setSubscribe: (email: string) => {
    return http.post<{ status: boolean; message: string }>(`/post/subscribe`, {
      email,
    });
  },
  getHot: () => {
    return http.get<HotArticlesVo[]>("/post/hot");
  },
};
